package com.medical.admin.hospital.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.hospital.common.dto.HealthyEssayDto;
import com.medical.admin.hospital.common.vo.HealthyEssayVo;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.entity.HealthyEssay;
import com.medical.admin.hospital.mapper.HealthyEssayMapper;
import com.medical.admin.hospital.service.HealthyEssayService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class HealthyEssayServiceImpl implements HealthyEssayService {

    @Resource
    private HealthyEssayMapper healthyEssayMapper;

    /**
     * 动态分页查询健康百科信息
     * @param healthyEssayTitle
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<HealthyEssayVo> list(String healthyEssayTitle, int pageNum, int pageSize) {
        Page<HealthyEssay> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<HealthyEssay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(healthyEssayTitle!=null,HealthyEssay::getHealthyEssayTitle,healthyEssayTitle);
        Page<HealthyEssay> healthyEssayPage = healthyEssayMapper.selectPage(page, queryWrapper);
        //判断数据是否为空
        if (ObjectUtil.isEmpty(healthyEssayPage.getRecords())){
            throw new BaseException(ResponseCode.HEALTHY_ESSAY_NOT_EXIST_ERROR);
        }
        PageVo<HealthyEssayVo> healthyEssays = new PageVo<>();
        healthyEssays.setRows(BeanConvertUtils.convertListTo(healthyEssayPage.getRecords(),HealthyEssayVo::new));
        healthyEssays.setTotal(healthyEssayPage.getRecords().size());
        return healthyEssays;
    }

    /**
     * 新增健康百科信息
     * @param healthyEssayDto
     * @return
     */
    @Override
    public Integer insert(HealthyEssayDto healthyEssayDto) {
        HealthyEssay healthyEssay = findHealthyEssay(healthyEssayDto);
        //判断数据是否存在
        if (ObjectUtil.isNotEmpty(healthyEssay)){
            throw new BaseException(ResponseCode.HOSPITAL_EXIST_ERROR);
        }
        int count = healthyEssayMapper.insert(BeanConvertUtils.convertTo(healthyEssayDto, HealthyEssay::new));
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }

    /**
     * 修改健康百科信息
     * @param healthyEssayDto
     * @return
     */
    @Override
    public Integer update(HealthyEssayDto healthyEssayDto) {
        HealthyEssay healthyEssay = healthyEssayMapper.selectById(healthyEssayDto.getHealthyEssayId());
        Optional.ofNullable(healthyEssay).orElseThrow(()->new BaseException(ResponseCode.HEALTHY_ESSAY_NOT_EXIST_ERROR));
        //判断数据是否被删除
        if (healthyEssay.getState()!=1){
            throw new BaseException(ResponseCode.HEALTHY_ESSAY_NOT_EXIST_ERROR);
        }
        return healthyEssayMapper.updateById(BeanConvertUtils.convertTo(healthyEssayDto,HealthyEssay::new));
    }

    /**
     * 删除健康百科信息
     * @param healthyEssayId
     * @return
     */
    @Override
    public Integer delete(Long healthyEssayId) {
        HealthyEssay healthyEssay = healthyEssayMapper.selectById(healthyEssayId);
        Optional.ofNullable(healthyEssay).orElseThrow(()->new BaseException(ResponseCode.HEALTHY_ESSAY_NOT_EXIST_ERROR));
        healthyEssay.setState(0);
        return healthyEssayMapper.updateById(healthyEssay);
    }

    /**
     * 查询健康百科信息详情
     * @param healthyEssayId
     * @return
     */
    @Override
    public HealthyEssayVo select(Long healthyEssayId) {
        HealthyEssay healthyEssay = healthyEssayMapper.selectById(healthyEssayId);
        if (ObjectUtil.isEmpty(healthyEssay)){
            throw new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        //判断数据是否被删除
        if (healthyEssay.getState()!=1){
            throw new BaseException(ResponseCode.HOSPITAL_NOT_EXIST_ERROR);
        }
        return  BeanConvertUtils.convertTo(healthyEssay,HealthyEssayVo::new);
    }

    /**
     * 查询健康百科信息
     * @param healthyEssayDto
     * @return
     */
    public HealthyEssay findHealthyEssay(HealthyEssayDto healthyEssayDto){
        //通过标题判断信息是否存在
        LambdaQueryWrapper<HealthyEssay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HealthyEssay::getHealthyEssayTitle,healthyEssayDto.getHealthyEssayTitle());
        return healthyEssayMapper.selectOne(queryWrapper);
    }
}
